const std = @import("std");

pub fn build(b: *std.Build) !void {
    // build all projects
    for (b.available_deps) |available_dep| {
        const dep_name, _ = available_dep;
        const dep = b.dependency(dep_name, .{});

        const os = dep.artifact("os");
        const install_os = b.addInstallFile(os.getEmittedBin(), dep_name);
        b.getInstallStep().dependOn(&install_os.step);
    }

    // other steps
    var run_tls = b.step("run", "Run os in QEMU");
    var qemu_tls = b.step("qemu", "Run os in QEMU with gdb server");
    var gdb_tls = b.step("gdb", "Debug with gdb");
    var code_tls = b.step("code", "Display code information");

    if (b.args == null or b.args.?.len < 1) return;

    for (b.available_deps) |available_dep| {
        const dep_name, _ = available_dep;
        if (!std.mem.startsWith(u8, dep_name, b.args.?[0])) continue;
        const dep = b.dependency(dep_name, .{});
        const top_level_steps = dep.builder.top_level_steps;

        const run_step = top_level_steps.get("run").?;
        const qemu_step = top_level_steps.get("qemu").?;
        const gdb_step = top_level_steps.get("gdb").?;
        const code_step = top_level_steps.get("code").?;

        run_tls.dependOn(&run_step.step);
        qemu_tls.dependOn(&qemu_step.step);
        gdb_tls.dependOn(&gdb_step.step);
        code_tls.dependOn(&code_step.step);
        break;
    }
}
